package com.gitee.cui.concurrency.syncontainer;

import com.gitee.cui.util.Print;
import com.gitee.cui.util.ThreadUtil;

import java.util.Collections;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.CountDownLatch;

/**
 * collections静态方法构建同步容器测试类
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/12/1 14:38
 **/
public class CollectionsDemo {

    public static void main(String[] args) throws InterruptedException {
        SortedSet<String> elementSet = new TreeSet<>();
        // 增加元素
        elementSet.add("element 1");
        elementSet.add("element 2");

        // 将elementSet包装成一个同步容器
        SortedSet<String> sortedSet = Collections.synchronizedSortedSet(elementSet);
        // 输出容器中的元素
        System.out.println("SortedSet is : " + sortedSet);
        CountDownLatch latch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            ThreadUtil.getCpuIntenseTargetThreadPool().submit(() -> {
                sortedSet.add("element " + (3 + finalI));
                Print.tco("add element " + (3 + finalI));
                latch.countDown();
            });
        }

        latch.await();
        System.out.println("SortedSet is : " + sortedSet);
    }
}
